package craky.keeper.skin;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;

import craky.componentc.JCButton;
import craky.keeper.EmptyComponent;
import craky.keeper.KeeperApp;
import craky.keeper.util.KeeperUtil;

public class SkinUnit extends JComponent {
	private static final long serialVersionUID = 4145544490659464008L;
	private static final Color SHADOW_COLOR = new Color(255, 255, 255, 60);

	private static final Color SELECTED_COLOR = new Color(255, 255, 255);
	private KeeperApp keeper;
	private SkinMgr skinMgr;
	private Skin skin;
	private JCButton btnAdd;
	private JCButton btnRemove;
	private EmptyComponent ec;
	private boolean first;
	private boolean last;
	private boolean mouseIn;

	public SkinUnit(KeeperApp keeper, SkinMgr skinMgr) {
		this.keeper = keeper;
		this.skinMgr = skinMgr;
		this.btnAdd = new JCButton();
		this.btnRemove = new JCButton();
		this.ec = new EmptyComponent();
		ActionListener action = new ActionHandle();
		MouseListener mouseHandle = new MouseHandle();
		init();
		addMouseListener(mouseHandle);
		this.btnAdd.addActionListener(action);
		this.btnAdd.addMouseListener(mouseHandle);
		this.btnRemove.addActionListener(action);
		this.btnRemove.addMouseListener(mouseHandle);
	}

	private void init() {
		setLayout(new BorderLayout());
		this.ec.setLayout(new BorderLayout());
		this.ec.setPreferredSize(new Dimension(-1, 13));
		this.ec.setVisible(false);
		this.btnAdd.setToolTipText("请点击择张图片，生成属于您自己风格的皮肤");

		this.btnAdd.setFocusable(false);
		this.btnAdd.setImage(KeeperUtil.getImage("skin_add_normal.png", true));
		this.btnAdd.setRolloverImage(KeeperUtil.getImage(
				"skin_add_rollover.png", true));
		this.btnAdd.setPressedImage(this.btnAdd.getRolloverImage());
		this.btnAdd.setVisible(false);
		this.btnRemove.setToolTipText("删除");
		this.btnRemove.setPreferredSize(new Dimension(13, -1));
		this.btnRemove.setRolloverEnabled(false);
		this.btnRemove.setFocusable(false);
		this.btnRemove.setImage(KeeperUtil.getImage("skin_delete.png", true));
		this.btnRemove.setPressedImage(null);
		this.ec.add(this.btnRemove, "East");
		add(this.btnAdd, "Center");
		add(this.ec, "North");
	}

	@Override
	protected void paintComponent(Graphics g) {
		if (this.skin != null) {
			int width = getWidth();
			int height = getHeight();
			Image image = this.skin.getPreviewImage();
			g.drawImage(image, 0, 0, width, height, this);
			image = null;

			if (this.mouseIn) {
				g.setColor(SHADOW_COLOR);
				g.fillRect(0, 0, width, height);
			}

			if (this.skin.isSelected()) {
				g.setColor(SELECTED_COLOR);
				g.drawRect(0, 0, width - 1, height - 1);
			}
		}
	}

	public Skin getSkin() {
		return this.skin;
	}

	public void setSkin(Skin skin) {
		this.skin = skin;
		setToolTipText(skin != null ? skin.getName() : null);
		this.btnAdd.setVisible((this.last) && (skin == null));
		repaint();

		if (skin != null) {
			skin.setUnit(this);
			setFirst(skin.isDefault());
		}
	}

	public boolean isFirst() {
		return this.first;
	}

	public void setFirst(boolean first) {
		this.first = first;
	}

	public boolean isLast() {
		return this.last;
	}

	public void setLast(boolean last) {
		this.last = last;
		this.btnAdd.setVisible((last) && (this.skin == null));
	}

	private class ActionHandle implements ActionListener {
		private ActionHandle() {
		}

		public void actionPerformed(ActionEvent e) {
			Object source = e.getSource();

			if (source == SkinUnit.this.btnAdd) {
				KeeperUtil.switchToSystemLAFTemp(new AbstractAction() {
					private static final long serialVersionUID = 5648508246819086152L;

					public void actionPerformed(ActionEvent e) {
						SkinUnit.this.skinMgr.addSkin();
					}
				});
			} else if (source == SkinUnit.this.btnRemove) {
				SkinUnit.this.skinMgr.removeSkin(SkinUnit.this.skin, true);
			}
		}
	}

	private class MouseHandle extends MouseAdapter {
		private MouseHandle() {
		}

		@Override
		public void mouseEntered(MouseEvent e) {
			Object source = e.getSource();
			SkinUnit.this.mouseIn = true;
			SkinUnit.this.ec.setVisible((SkinUnit.this.skin != null)
					&& (!SkinUnit.this.first));

			if (SkinUnit.this.skin != null) {
				SkinUnit.this.repaint();
			}

			if ((source == SkinUnit.this) || (source == SkinUnit.this.btnAdd)) {
				SkinUnit.this.keeper.changeSkin(
						SkinUnit.this.skin == null ? SkinUnit.this.keeper
								.getCurrentSkin() : SkinUnit.this.skin, false);
			}
		}

		@Override
		public void mouseExited(MouseEvent e) {
			Object source = e.getSource();
			SkinUnit.this.mouseIn = false;
			SkinUnit.this.ec.setVisible(false);

			if (SkinUnit.this.skin != null) {
				SkinUnit.this.repaint();

				if (((source == SkinUnit.this) || (source == SkinUnit.this.btnRemove))
						&& (SkinUnit.this.skinMgr.needChange(e.getPoint(),
								(Container) source))) {
					SkinUnit.this.keeper.changeSkin(
							SkinUnit.this.keeper.getCurrentSkin(), false);
				}
			}
		}

		@Override
		public void mousePressed(MouseEvent e) {
			if ((SkinUnit.this.skin != null)
					&& (e.getSource() == SkinUnit.this)
					&& (SwingUtilities.isLeftMouseButton(e))) {
				SkinUnit.this.keeper.changeSkin(SkinUnit.this.skin, true);
			}
		}
	}
}